package com.ruoyi.space.controller;

import com.ruoyi.space.util.Result;
import com.ruoyi.space.util.UploadUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
@CrossOrigin("*")
public class UploadController {
    @RequestMapping("/fileUpload")
    public Result fileUpload(MultipartFile multipartFile) {
//        Map<String, String> result = new HashMap<>();
        try {
            //输入流
            InputStream inputStream = multipartFile.getInputStream();
            //获取文件后缀名称
            String fileName = multipartFile.getOriginalFilename();
            //后缀
            String subFix = fileName.substring(fileName.lastIndexOf("."));
            //生成一个不重复的文件名
            String realName = UUID.randomUUID().toString() + subFix;

            UploadUtil.fileUpload(realName, inputStream);

//            result.put("status", "OK");
//            result.put("msg","上传成功");
//            result.put("url", "http://file-upload101.oss-cn-beijing.aliyuncs.com/" + realName);

            return Result.success()
                    .put("随便的key","随便的value")
                    .put("url", "http://file-upload101.oss-cn-beijing.aliyuncs.com/" + realName);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
}
